package wiki.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import wiki.generic.contents.Content;
import wiki.generic.io.Element;
import wiki.generic.utilities.PageNotFoundException;
import wiki.setup.Setup;
/**
 * 
 * @author <p><strong>JwTeam</strong></p> 
 * Responsabile della modifica dei testi
 */
public class TextModifyServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;



	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			HttpSession ses=req.getSession(false);
			if(ses!=null){
				boolean isDoc = (Boolean) ses.getAttribute("rights");
				if(isDoc){
			String name=req.getParameter("name");
			String type=req.getParameter("type");
			int ID=Integer.parseInt(req.getParameter("ID"));
			String corso=(String) ses.getAttribute("corso");
			String current=(String) ses.getAttribute("current");
			List<String> oldAttr= new ArrayList<String>();
			try {
				Content old=Setup.getSetup().getMediator().getpService().getContentbyName(corso, current, name);
				oldAttr=old.getAttributes();
			} catch (PageNotFoundException e) {
				resp.sendRedirect("./html/errorpages/pagina404.html");
			}
			
			PrintWriter out=resp.getWriter();
			out.write("<html>\n");
			out.write("<head>\n");
			out.write("<title>Inserisci Testo</title>\n");
			out.write("<link rel='stylesheet' type='text/css' href='./html/edit/scegli2.css' />\n ");
			out.write("<script type='text/JavaScript' src='./html/edit/scegli2.js'></script>\n");
			out.write("</head>\n");
			out.write("<body>\n");
			out.write("<div id='title'>\n");
			out.write("Modifica:</div>\n");
			String[][] sizes=new String [6][2];
			if(type.equals("p")){
				out.write("<div id='p-container'>\n");
				out.write("<form action='./tmod?type=p&name="+name+"&ID="+ID+"' method='post' name='modulo_paragrafo' id='modulo_paragrafo'>\n");
				out.write("<div id='show_message_paragrafo'>I seguenti campi sono obbligatori*:</div>\n");
				out.write("<h3>Modifica Paragrafo<span id='star_testo'>*</span>:</h3>\n");
				out.write("<input type='button' id='link-button' value='link' onclick=\"setVisibleElement('link-container')\"/>\n");
				out.write("<input type='button' id='b-button' value='b' onclick=\"addStyle('<strong></strong>')\"/>\n");
				out.write("<input type='button' id='i-button' value='i' onclick=\"addStyle('<em></em>')\"/>\n");
				out.write("<input type='button' id='u-button' value='u' onclick=\"addStyle('<u></u>')\"/>\n");
				out.write("<div id='link-container'>\n");
				out.write("<table>\n");
				out.write("<tr>\n");
				out.write("<td class='nomelink'><strong>Nome:</strong></td>\n");
				out.write("<td class='inputlink'><input id='input_nome_link' class='testo' type='text' name='nome-link' value=''></td>\n");
				out.write("</tr>");
				out.write("<tr>");
				out.write("<td class='nomelink'><strong>Url</strong>&nbsp;http://</td>\n");
				out.write("<td><input id='input_url' class='testo' type='text' name='url-link' value=''/></td> \n");
				out.write("</tr>\n");
				out.write("</table>\n");
				out.write("<input type='button' value='Aggiungi' id='bottone2' name='invia' onclick=\"addLink('input_nome_link','input_url');setVisibleElement('link-container');\" \n/> ");
				out.write("</div>\n");
				out.write("<textarea cols='100' rows='50' id='area' name='testo' value='' ");
				Element el=Setup.getSetup().getMediator().getVservice().getContentMainParameterbyID(type, ID);
				String value=el.getInfo();
				if(el.getOwner().compareTo(corso)!=0){
					out.write("style='display:none'");	
				}
				out.write(">"+value+"</textarea>\n"); 
				if(el.getOwner().compareTo(corso)!=0){
					out.write("<textarea disabled='disabled' cols='100' rows='50' id='area' name='testo' value=''>"+value+"</textarea>\n");
				}
				out.write("<br/>");
				out.write("<br/>");
				out.write("<br/>");
				out.write("<input type='submit' value='Aggiungi' id='bottone' name='submit' onclick=\"sostituisci('modulo_paragrafo')\" />");
				out.write("<br/>");
				sizes[0][0]="font-size:14px";
				sizes[1][0]="font-size:16px";
				sizes[2][0]="font-size:18px";
				sizes[3][0]="font-size:20px";
				sizes[4][0]="font-size:22px";
				sizes[5][0]="font-size:24px";
				sizes[0][1]="14";
				sizes[1][1]="16";
				sizes[2][1]="18";
				sizes[3][1]="20";
				sizes[4][1]="22";
				sizes[5][1]="24";
			}
			if(type.equals("h3")){
				out.write("<div id='title-container'>\n");
				out.write("<form action='./tmod?type=h3&name="+name+"&ID="+ID+"'method='post' name='modulo_titolo' id='modulo_titolo'>\n");
				
				out.write("<div id='show_message_titolo'>I seguenti campi sono obbligatori*:</div>\n");
			
				out.write("<h3>Titolo<span id='star_testo'>*</span>:&nbsp;&nbsp;&nbsp;\n");
				out.write("<input class='testo' name='testo' value='");
				Element el=Setup.getSetup().getMediator().getVservice().getContentMainParameterbyID(type, ID);
				String value=el.getInfo();
				out.write(value+"' ");      
				if(el.getOwner().compareTo(corso)!=0){
					out.write("type='hidden'");
					} else{
						out.write("type='text'");
					}
				out.write("/></h3>\n");
				if(el.getOwner().compareTo(corso)!=0){
					out.write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input class='testo' name='abs' disabled='disabled' value='"+el.getInfo()+"'/>");
					out.write("</br>");
				}
				out.write("<br/>");
				out.write("<input type='submit' value='Aggiungi' id='bottone' name='submit'/>");
				out.write("<br/>");
				sizes[0][0]="font-size:20px";
				sizes[1][0]="font-size:22px";
				sizes[2][0]="font-size:24px";
				sizes[3][0]="font-size:26px";
				sizes[4][0]="font-size:28px";
				sizes[5][0]="font-size:30px";
				sizes[0][1]="20";
				sizes[1][1]="22";
				sizes[2][1]="24";
				sizes[3][1]="26";
				sizes[4][1]="28";
				sizes[5][1]="30";
			}
				out.write("<div id='style-contents'>");
				out.write("<h3 id='h3Color'>Dimensione Font:</h3>\n");
				out.write("<select name='font-size'>\n");	
				int pos=getOldAttrPosition(oldAttr, "font-size");
				for (int i = 0; i < 6; i++) {
						out.write("<option value='"+sizes[i][0]+"' ");
						if((pos>-1 && oldAttr.get(pos).equals(sizes[i][0]))){
							out.write("selected='selected' ");
						}
						out.write(">"+sizes[i][1]+"</option>\n");
					}
			
				out.write("</select>\n");
				out.write("<br />");
				out.write("<br />");

				out.write("<h3 id='h3Color'>Color:</h3>\n");
				out.write("<table id='color-table'>\n");
				out.write("<tr>\n");
				pos=getOldAttrPosition(oldAttr, "color");
				String[][] supportedColors=new String[10][2];
				supportedColors[0][0]="black";
				supportedColors[1][0]="yellow";
				supportedColors[2][0]="orange";
				supportedColors[3][0]="red";
				supportedColors[4][0]="pink";
				supportedColors[5][0]="violet";
				supportedColors[6][0]="blue";
				supportedColors[7][0]="light-blue";
				supportedColors[8][0]="light-green";
				supportedColors[9][0]="green";
				supportedColors[0][1]="#000";
				supportedColors[1][1]="#FF0";
				supportedColors[2][1]="#F60";
				supportedColors[3][1]="#F00";
				supportedColors[4][1]="#F9C";
				supportedColors[5][1]="#93F";
				supportedColors[6][1]="#03C";
				supportedColors[7][1]="#00CCFF";
				supportedColors[8][1]="#3F0";
				supportedColors[9][1]="#090";
				
				for (int i = 0; i < 10; i++) {
					out.write("<td><div id='"+supportedColors[i][0]+"'><input" +
							" type='radio' class='color' value='color:"+supportedColors[i][1]+"'" +
									" name='color' ");
					if(i==0 || (pos>-1 && oldAttr.get(pos).equals("color:"+supportedColors[i][1]))){
						out.write("checked='checked'");
					}
					out.write("/></div></td>\n");
				}
				
				out.write("</tr>\n");
				out.write("</table>\n");
				out.write("</div>\n");
				
				out.write("<h3>Font:</h3>\n");
				out.write("<ul id='fontstyle-list'>\n");
				
				pos=getOldAttrPosition(oldAttr, "font-family");
				String[] families={"Verdana","Arial","Tahoma"};
				
				for (int i = 0; i < families.length; i++) {
					out.write("<li><input type='radio' class='stile'  name='font' value='font-family:"+families[i]+"' ");
					if(i==0 || (pos>-1 && oldAttr.get(pos).equals("font-family:"+families[i]))){
						out.write("checked='checked' ");
					}
					out.write("/><span style='font-family:"+families[i]+"' >"+families[i]+"</span></li>\n");
				}
				
				out.write("</ul>\n");
				out.write("<h3>Font-Style:</h3>\n");
				out.write("<ul id='fontstyle-list'>\n");
				
				pos=getOldAttrPosition(oldAttr, "font-style");
				String[] styles={"Normal","Italic"};
				out.write(writeAttr(pos, styles, oldAttr, "stile","font-style"));
				out.write("</ul>\n");
				out.write("<h3>Font-Weight:</h3>\n");
				out.write("<ul id='fontstyle-list'>\n");
				
				pos=getOldAttrPosition(oldAttr, "font-weight");
				String[] w={"Normal","Bold"};
				out.write(writeAttr(pos, w, oldAttr, "weight","font-weight"));
				
				out.write("</ul>\n");
				out.write("<h3>Text Decoration:</h3>\n");
				out.write("<ul id='fontstyle-list'>\n");
				
				pos=getOldAttrPosition(oldAttr, "text-decoration");
				String[] td={"Normal","Overline","Underline","Line-Through"};
				out.write(writeAttr(pos, td, oldAttr, "dec","text-decoration"));
			
				out.write("</ul>\n");
				
				out.write("<h3>Text-Align:</h3>\n");
				out.write("<ul id='fontstyle-list'>\n");
				pos=getOldAttrPosition(oldAttr, "text-align");
				String[] al=new String[4];
				if(type.equals("p")){
					al[0]="Justify";
					al[1]="Center";
					al[2]="Left";
					al[3]="Right";
				}
				if(type.equals("h3")){
					al[0]="Center";
					al[1]="Justify";
					al[2]="Left";
					al[3]="Right";
				}
				
				out.write(writeAttr(pos, al, oldAttr,"al","text-align"));
				out.write("</ul>\n");
				out.write("<br/>");
				if(type.equals("p")){
					out.write("<input type='submit' value='Aggiungi' id='bottone' name='submit' onclick=\"sostituisci('modulo_paragrafo')\" /></form></div>");
				} else{
					out.write("<input type='submit' value='Aggiungi' id='bottone' name='submit'/></form></div>");
				}
		
				out.write("</body>");
				out.write("</html>");			
				} else{
					resp.sendRedirect("./html/errorpages/nopermessi.html");
					return;
				}
			} else{
					resp.sendRedirect("./html/LOGIN/login.html");
					return;
			}
				
	}
		
	private int getOldAttrPosition(List<String> attr, String at){
		for (int i = 0; i < attr.size(); i++) {
			if(attr.get(i).startsWith(at)){
				return i;
			}
			
		}
		return -1;
	}
	
	private String writeAttr(int pos, String[] possibilities,List<String> oldAttr, String name, String prop){
		String s="";
		for (int i = 0; i < possibilities.length; i++) {
			s+="<li><input type='radio' class='stile' value='"+prop+":"+possibilities[i].toLowerCase()+"' name='"+name+"' ";
			if(i==0 || (pos>-1 && oldAttr.get(pos).equals(prop+":"+possibilities[i].toLowerCase()))){
				s+=" checked='checked' ";
			}
			s+=" /><span style='"+prop+":"+possibilities[i].toLowerCase()+"'>"+possibilities[i]+"</span></li>\n";
		}
		
		return s;
	}
	
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession ses=req.getSession(false);
		if(ses!=null){
			boolean isDoc = (Boolean) ses.getAttribute("rights");
			if(isDoc){
		String corso=(String) ses.getAttribute("corso");
		String current=(String) ses.getAttribute("current");
		String nome=req.getParameter("name");
		if(nome==null || nome.equals("")){
			nome="___";
		}
		nome=nome.replace(" ", "_");
		String type=req.getParameter("type");
		int ID=Integer.parseInt(req.getParameter("ID"));
		String newPar=new String();
		if(req.getParameter("testo")!=null && req.getParameter("testo")!="" ){
			newPar=req.getParameter("testo");
			newPar=Setup.getSetup().getMediator().getGpService().changeHTMLText(newPar);
		} else {
			resp.sendRedirect("./html/errorpages/notesto.html");
			return;
		}
		List<String> attr=new ArrayList<String>();
		attr.add(req.getParameter("color"));
		attr.add(req.getParameter("font-size"));
		attr.add(req.getParameter("font"));
		attr.add(req.getParameter("stile"));
		attr.add(req.getParameter("weight"));
		attr.add(req.getParameter("dec"));
		attr.add(req.getParameter("al"));
		try {
			Setup.getSetup().getMediator().geteService().modifyIDElement(corso,current, nome, type, ID, newPar, attr);
			resp.sendRedirect("./view?page="+current);
		} catch (PageNotFoundException e) {
			resp.sendRedirect("./html/errorpages/pagina404.html");
		}
		}else{
			resp.sendRedirect("./html/errorpages/nopermessi.html");
			return;
			}
		} else {
			resp.sendRedirect("./hmtl/LOGIN/login.html");
		}
		
	} 
}
	
